﻿
using System;
using System.Windows.Forms;
using System.Drawing;
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using System.Diagnostics;

namespace FaceFromCamera
{
  
        public partial class Form1 : Form,IView
        {
            private Capture cap;
            private HaarCascade haar;
            Person recognizedPerson;
            bool logedIn = false;
            public Form1()
            {
                InitializeComponent();
            }

            private void timer1_Tick(object sender, EventArgs e)
            {
                RecognitionEngineControler.Instance.RaiseModelChange(this,null);
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                RecognitionEngineControler.Instance.RegisterView(this);
                try
                {
                    cap = new Capture(0);
                }
                catch (Exception exc)
                {
              
                    MessageBox.Show("Can't find camera", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Dispose();
                }
                try
                {
                    haar = new HaarCascade("haarcascade_frontalface_alt2.xml");
                }
                catch (Exception exc)
                {

                    MessageBox.Show("Can't find cascade file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Close();
                }
                
            }

            private void button2_Click(object sender, EventArgs e)
            {
                if (textBox2.Visible)
                {
                    
                    recognizedPerson = PersonControler.Instance.GetUser(textBox2.Text);
                }

                if (textBox1.Text == recognizedPerson.password)
                {
                    MessageBox.Show("Welcome " + recognizedPerson.name + "!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    logedIn = true;
                    editUserToolStripMenuItem.Visible = true;
                    if (RecognitionEngineControler.Instance.GetFaceFromFrame() != null && textBox2.Visible)
                    {
                        Image<Gray, byte> gr_temp = new Image<Gray, byte>(new Bitmap(PictureOperations.ResizeImage(new Bitmap(RecognitionEngineControler.Instance.GetFaceFromFrame()),64,64)));
                        gr_temp._EqualizeHist();
                        if(gr_temp.Width ==64 && gr_temp.Height==64)
                        PersonControler.Instance.AddPicture(gr_temp.ToBitmap(), recognizedPerson.id);
                    }
                }
                else
                {
                    MessageBox.Show("Authorization failed!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    logedIn = false;
                    editUserToolStripMenuItem.Visible = false;
                }
                
            }
            #region IView Members

            public void ModelChange(object sender, Person e)
            {
                
                    if (!textBox2.Visible)
                        pictureBox1.Image = RecognitionEngineControler.Instance.UpdateFrame(cap, haar);
                    if (RecognitionEngineControler.Instance.GetFaceFromFrame() != null)
                    {
                     
                        if(!logedIn)
                        recognizedPerson = RecognitionEngineControler.Instance.Recognize();
                        l_login.Text = "Login: " + recognizedPerson.login;
                        button2.Enabled = true;
                        timer2.Enabled = true;
                    }
                    else if (textBox2.Visible && textBox2.Text != "")
                    {
                        button2.Enabled = true;

                    }
                    else
                    {
                        l_login.Text = "Login: Unrecognized person";
                        button2.Enabled = false;
                        timer2.Enabled = false;
                    }
                
            }

            #endregion

            private void newUserToolStripMenuItem_Click(object sender, EventArgs e)
            {
                NewUser New = new NewUser();
                New.Show();
            }
            private void timer2_Tick(object sender, EventArgs e)
            {
                RecognitionEngineControler.Instance.ResetFace();
                
            }
            private void editUserToolStripMenuItem_Click(object sender, EventArgs e)
            {
                NewUser New = new NewUser(recognizedPerson,PersonControler.Instance.UserPictures(recognizedPerson.id));
                New.Show();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                if (textBox2.Visible == true)
                {
                    logedIn = false;
                    textBox2.Visible = false;
                    ((Button)sender).Text = "It's not me";
                }
                else {
                    textBox2.Visible = true;

                    ((Button)sender).Text = "Recognize";
                }
            }

            private void exitToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }


